var api = require('../config/api.js');

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatDay(date){
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month, day].map(formatNumber).join('-') 
}


function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 封封微信的的request
 */
function request(url, data = {}, method = "GET") {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log("request success");

        if (res.statusCode == 200) {

          
            resolve(res.data);
          
        } else {
          reject(res.errMsg);
        }

      },
      fail: function (err) {
        reject(err)
        console.log("failed")
      }
    })
  });
}

/**
 * 检查微信会话是否过期
 */
function checkSession() {
  return new Promise(function (resolve, reject) {
    wx.checkSession({
      success: function () {
        resolve(true);
      },
      fail: function () {
        reject(false);
      }
    })
  });
}

/**
 * 调用微信登录
 */
function login() {
  return new Promise(function (resolve, reject) {
    wx.login({
      success: function (res) {
        if (res.code) {
          //登录远程服务器
          console.log(res)
          resolve(res);
        } else {
          reject(res);
        }
      },
      fail: function (err) {
        reject(err);
      }
    });
  });
}

function getUserInfo() {
  return new Promise(function (resolve, reject) {
    wx.getUserInfo({
      withCredentials: true,
      success: function (res) {
        console.log(res)
        resolve(res);
      },
      fail: function (err) {
        reject(err);
      }
    })
  });
}

function redirect(url) {

  //判断页面是否需要登录
  if (false) {
    wx.redirectTo({
      url: '/pages/auth/login/login'
    });
    return false;
  } else {
    wx.redirectTo({
      url: url
    });
  }
}

function showErrorToast(msg) {
  wx.showToast({
    title: msg,
    image: '/static/images/icon_error.png'
  })
}
 function formatFloat(f, digit) {
  var m = Math.pow(10, digit);
  return Math.round(f * m, 10) / m;
}

function checkBankNo(bankno) {
  var bankno = bankno.replace(/\s/g, '');
  if (bankno == "") {
    return false;
  }
  if (bankno.length < 12 || bankno.length > 19) {
    return false;
  }
  var num = /^\d*$/;// 全数字
  if (!num.exec(bankno)) {
    return false;
  }
  // 开头两位
  var strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";
  if (strBin.indexOf(bankno.substring(0, 2)) == -1) {
    return false;
  }
  // luhn校验
  if (!luhnCheck(bankno)) {
    return false;
  }
  return true;
}


//校验银行卡号
function luhnCheck(bankno) {
  var lastNum = bankno.substr(bankno.length - 1, 1);// 取出最后一位（与luhn进行比较）
  var first15Num = bankno.substr(0, bankno.length - 1);// 前15或18位
  var newArr = new Array();
  for (var i = first15Num.length - 1; i > -1; i--) { // 前15或18位倒序存进数组
    newArr.push(first15Num.substr(i, 1));
  }
  var arrJiShu = new Array(); // 奇数位*2的积 <9
  var arrJiShu2 = new Array(); // 奇数位*2的积 >9
  var arrOuShu = new Array(); // 偶数位数组
  for (var j = 0; j < newArr.length; j++) {
    if ((j + 1) % 2 == 1) {// 奇数位
      if (parseInt(newArr[j]) * 2 < 9) {
        arrJiShu.push(parseInt(newArr[j]) * 2);
      } else {
        arrJiShu2.push(parseInt(newArr[j]) * 2);
      }
    } else {
      arrOuShu.push(newArr[j]);// 偶数位
    }
  }

  var jishu_child1 = new Array();// 奇数位*2 >9 的分割之后的数组个位数
  var jishu_child2 = new Array();// 奇数位*2 >9 的分割之后的数组十位数
  for (var h = 0; h < arrJiShu2.length; h++) {
    jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
    jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
  }
  var sumJiShu = 0; // 奇数位*2 < 9 的数组之和
  var sumOuShu = 0; // 偶数位数组之和
  var sumJiShuChild1 = 0; // 奇数位*2 >9 的分割之后的数组个位数之和
  var sumJiShuChild2 = 0; // 奇数位*2 >9 的分割之后的数组十位数之和
  var sumTotal = 0;
  for (var m = 0; m < arrJiShu.length; m++) {
    sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
  }
  for (var n = 0; n < arrOuShu.length; n++) {
    sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
  }
  for (var p = 0; p < jishu_child1.length; p++) {
    sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
    sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
  }
  // 计算总和
  sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu)
    + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
  // 计算luhn值
  var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
  var luhn = 10 - k;
  if (lastNum == luhn) {
    console.log("验证通过");
    return true;
  } else {
    return false;
  }
}

module.exports = {
  formatTime,
  formatDay,
  request,
  redirect,
  showErrorToast,
  checkSession,
  login,
  getUserInfo,
  formatFloat,
  luhnCheck,
  checkBankNo
}


